import os
import json
import time
from datetime import datetime
import logging
import csv 
def save_raw_data_to_csv(raw_posts, filename=r"D:\Users\Lenovo\Desktop\作业4\public_opinion.csv"):
    """将原始数据存储到CSV文件"""
    try:
        # 确保目录存在
        os.makedirs(os.path.dirname(filename), exist_ok=True)
        
        # 检查文件是否存在，决定是否写入表头
        file_exists = os.path.isfile(filename)
        
        # 写入CSV文件（追加模式）
        with open(filename, 'a', newline='', encoding='utf-8-sig') as csvfile:
            fieldnames = ['id', 'raw_json', 'stored_at']
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            
            # 如果文件不存在，写入表头
            if not file_exists:
                writer.writeheader()
            
            # 获取当前时间
            stored_at = datetime.now().isoformat()
            
            # 写入数据
            for post in raw_posts:
                post_id = post.get('id', '')
                raw_json = json.dumps(post, ensure_ascii=False)
                
                writer.writerow({
                    'id': post_id,
                    'raw_json': raw_json,
                    'stored_at': stored_at
                })
        
        logging.info(f"成功存储 {len(raw_posts)} 条原始数据到CSV文件 {filename}")
        return True
    except Exception as e:
        logging.error(f"原始数据CSV存储失败: {str(e)}")
        
        # 尝试简单备份
        try:
            backup_name = f"raw_backup_{int(time.time())}.csv"
            with open(backup_name, 'w', encoding='utf-8-sig') as f:
                f.write("id,text\n")
                for post in raw_posts:
                    f.write(f"{post.get('id','')},{post.get('text','')}\n")
            logging.warning(f"创建原始数据简易备份: {backup_name}")
        except:
            logging.error("原始数据备份失败")
        
        return False